package com.sgb.link.view.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import androidx.recyclerview.widget.GridLayoutManager;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.luck.picture.lib.entity.LocalMedia;
import com.sgb.link.R;
import com.sgb.link.databinding.ItemSelfAddPicBinding;
import com.swgk.core.BR;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import java.util.ArrayList;
import java.util.List;

public class SelfTeamPicAdapter extends BaseRecycleAdapter<String> {

    private static final String TAG_ADD = "add";

    private int mMaxSize;

    private GridLayoutManager mGridLayoutManager;

    public SelfTeamPicAdapter(Context context, List<String> datas, int maxSize, GridLayoutManager mGridLayoutManager) {
        super(context, R.layout.item_self_add_pic, datas);
        mMaxSize = maxSize;
        this.mGridLayoutManager = mGridLayoutManager;
        if (getDatas().size() < mMaxSize) {
            getDatas().add(TAG_ADD);
        }
    }

    @Override
    public int getItemCount() {
        return getDatas().size();
    }

    @Override
    public void onBindViewHolder(ItemViewHolder holder, @SuppressLint("RecyclerView") int position) {
        holder.getmBinding().getRoot().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (position >= getRealCount()) {
                    if (onItemClick != null) {
                        onItemClick.onItemClick("", position);
                    }
                } else {
                    if (onItemClick != null) {
                        onItemClick.onItemClick(datas.get(position), position);
                    }
                }
            }
        });
        if (getRealCount() == mMaxSize && position == mMaxSize) {
            ((ItemSelfAddPicBinding) holder.getmBinding()).llAdd.setVisibility(View.GONE);
        } else {
            ((ItemSelfAddPicBinding) holder.getmBinding()).llAdd.setVisibility(View.VISIBLE);
        }
        try {
            convert(holder, datas.get(position));
        } catch (IndexOutOfBoundsException e) {
            e.fillInStackTrace();
        }
    }

    @Override
    public void convert(ItemViewHolder holder, final String entity) {
        holder.setBinding(BR.viewModel, entity);
        if (!TextUtils.equals(entity, TAG_ADD)) {
            ((ItemSelfAddPicBinding) holder.getmBinding()).rlDelete.setTag(holder.getAdapterPosition());
            ((ItemSelfAddPicBinding) holder.getmBinding()).rlDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (getRealCount() == mMaxSize) {
                        getDatas().remove(holder.getAdapterPosition());
                        getDatas().add(TAG_ADD);
                    } else {
                        getDatas().remove(holder.getAdapterPosition());
                    }
                    notifyDataSetChanged();
                }
            });
            ((ItemSelfAddPicBinding) holder.getmBinding()).rlDelete.setVisibility(View.VISIBLE);
            RequestOptions options = new RequestOptions();
            options.placeholder(R.drawable.null_img_list);
            options.override(400, 400);
            options.transforms(new CenterCrop(), new RoundedCorners(8));
            Glide.with(holder.itemView.getContext())
                    .load(entity)
                    .apply(options)
                    .into(((ItemSelfAddPicBinding) holder.getmBinding()).ivPicture);
            ((ItemSelfAddPicBinding) holder.getmBinding()).ivAdd.setVisibility(View.GONE);
        } else {
            ((ItemSelfAddPicBinding) holder.getmBinding()).ivAdd.setVisibility(View.VISIBLE);
            ((ItemSelfAddPicBinding) holder.getmBinding()).rlDelete.setVisibility(View.GONE);
            ((ItemSelfAddPicBinding) holder.getmBinding()).ivPicture.setImageResource(0);
        }

        ViewGroup.LayoutParams parm = holder.itemView.getLayoutParams();
/*        parm.height =
                mGridLayoutManager.getWidth() / mGridLayoutManager.getSpanCount()
                        - 2 * holder.itemView.getPaddingLeft() - 2 * ((ViewGroup.MarginLayoutParams) parm).leftMargin;*/
    }

    public int getRealCount() {
        for (String data : getDatas()) {
            if (TAG_ADD.equals(data)) {
                return getDatas().size() - 1;
            }
        }
        return getDatas().size();
    }

    public int hasCount() {
        return mMaxSize - getRealCount();
    }

    public void setPicData(List<LocalMedia> mediaList) {
        if (mediaList != null && mediaList.size() > 0) {
            for (int i = mediaList.size() - 1; i >= 0; i--) {
                getDatas().add(0, mediaList.get(i).getRealPath());
            }
            if (getRealCount() == mMaxSize) {
                getDatas().remove(TAG_ADD);
            }
            notifyDataSetChanged();
        }
    }

    public void setData(List<String> datas) {
        if (datas != null && datas.size() > 0) {
            getDatas().addAll(0, datas);
            if (getRealCount() == mMaxSize) {
                getDatas().remove(TAG_ADD);
            }
        } else {
            getDatas().clear();
            getDatas().add(TAG_ADD);
        }
        notifyDataSetChanged();
    }

    public List<String> getRealData(){
        List<String> data = new ArrayList<>(datas);
        if ("add".equals(data.get(data.size() -1))){
            data.remove(data.size() - 1);
        }

        return data;
    }
}
